//
//  KLSplashView.m
//  Launch
//
//  Created by Tommy on 15/4/7.
//  Copyright (c) 2015年 com.taobao. All rights reserved.
//

#import "KLSplashView.h"
#import "UIView+KLWhenTappedBlocks.h"
#import "Masonry.h"
#import "extscope.h"
#import "NSTimer+KLBlocks.h"
#import "Koala.h"

#define KLSplashViewAnimationInterval 0.3f
#define KLSplashViewDismissInterval   1.5f

#define SkipBtnSize   CGSizeMake(30, 20)

@interface KLSplashView()
@property(nonatomic,strong)UIImage  *image;
@property(nonatomic,copy) KLSplashViewTapHandler tapHandler;
@property(nonatomic,strong)NSTimer *timer;


@end

@implementation KLSplashView

+ (KLSplashView *)splashViewWithImage:(UIImage*)image andHandler:(KLSplashViewTapHandler)tapHandler{
    return [[KLSplashView alloc]initWithImage:image andHandler:tapHandler];
}

+ (KLSplashView *)showSplashViewWithImage:(NSString*)imageName andHandler:(KLSplashViewTapHandler)tapHandler{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if(!keyWindow){
        keyWindow = [[UIApplication sharedApplication].windows lastObject];
    }
    return [self showSplashViewOn:keyWindow withImage:imageName andHandler:tapHandler];
    
}

+ (KLSplashView *)showSplashViewOn:(UIView *)parentView withImage:(NSString*)imageName andHandler:(KLSplashViewTapHandler)tapHandler{
    KLSplashView *splashView = [KLSplashView splashViewWithImage:[UIImage imageNamed:imageName] andHandler:tapHandler];

    [parentView addSubview:splashView];
    [parentView bringSubviewToFront:splashView];
    @weakify(parentView);
    [splashView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(parentView);
        make.edges.equalTo(parentView);
    }];
    [splashView showAnimation];
    
    return splashView;
}


+ (void)presentSplashViewWithImage:(NSString*)imageName andHandler:(KLSplashViewTapHandler)tapHandler{

    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.view.backgroundColor = [UIColor whiteColor];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if(!keyWindow){
        keyWindow = [[UIApplication sharedApplication].windows lastObject];
    }
    
    UIViewController *rootViewController = keyWindow.rootViewController;
    KLSplashViewTapHandler tmpTapHandler = ^(KLSplashView *view){
        if(tapHandler && view){
            tapHandler(view);
        }
        [viewController dismissViewControllerAnimated:NO completion:nil];
    };
    KLSplashView *splashView = [self showSplashViewOn:viewController.view withImage:imageName andHandler:tmpTapHandler];
    splashView.skipBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        if(splashView.tapHandler)
            splashView.tapHandler(nil);
        return [RACSignal empty];
    }];
    
    
    [rootViewController.view addSubview:viewController.view];
    viewController.view.alpha = 0.1f;
    [UIView animateWithDuration:KLSplashViewAnimationInterval animations:^{
        viewController.view.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [viewController.view removeFromSuperview];
        [rootViewController presentViewController:viewController animated:NO completion:^{
            
        }];
    }];

    
}


- (instancetype)initWithImage:(UIImage*)image andHandler:(KLSplashViewTapHandler)tapHandler{
    if(self = [super init]){
        _image = image;
        _tapHandler = tapHandler;
        [self setupSubViews];
        @weakify(self);
        _timer = [NSTimer scheduledTimerWithTimeInterval:KLSplashViewDismissInterval repeats:NO block:^(NSTimer *timer) {
            @strongify(self);
            [self.skipBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
            [timer invalidate];
        }];
    }
    
    return self;
}

- (void)dealloc{
    [_timer invalidate];
    _timer = nil;
}

- (void)setupSubViews{
    self.alpha = 0.1f;
    self.imageView = [[UIImageView alloc]initWithImage:_image];
//    self.imageView.backgroundColor = [UIColor whiteColor];
    self.skipBtn   = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.skipBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [self.skipBtn setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    self.skipBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [self addSubview:self.imageView];
    [self addSubview:self.skipBtn];
    
    
    @weakify(self);
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.edges.equalTo(self);
    }];
    
    [_skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self).with.offset(20);
        make.right.equalTo(self).with.offset(-10);
        make.size.mas_equalTo(SkipBtnSize);
    }];
    

    self.skipBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self hideAnimation];
        return [RACSignal empty];
    }];
    [self.imageView whenTapped:^{
        @strongify(self);
        if(self.tapHandler){
            self.tapHandler(self);
        }
        [self hideAnimation];
    }];
    
}



- (void)showAnimation{
    [UIView animateWithDuration:KLSplashViewAnimationInterval animations:^{
        self.alpha = 1.0f;
    }];
}
- (void)hideAnimation{
    [UIView animateWithDuration:KLSplashViewAnimationInterval animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
